 <template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div class="search" v-show="pd.showSearch">
        <el-form :model="pd.queryParams" ref="queryFormRef" :inline="true" label-width="68px">
          <el-form-item label="仓库" prop="whId">
            <WarehouseSelect :params="{parentId: '0'}" v-model:wh-id="pd.queryParams.whId" placeholder="请选择仓库" @keyup.enter="handleQuery"/>
          </el-form-item>
          <el-form-item label="存储区域" prop="areaId">
            <WarehouseSelect :is-area="true" v-model:wh-id="pd.queryParams.areaId" placeholder="请选择存储区域" @keyup.enter="handleQuery"/>
          </el-form-item>
          <el-form-item label="产品名称" prop="productName">
            <ProductSearch :data-text="pd.queryParams.productName" :searchType="1" :params="{isStock:true}" @ok="lineAdd" title="产品查询"/>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <right-toolbar v-model:showSearch="pd.showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="pd.loading" :data="pd.WWPList">
        <el-table-column label="仓库名称" align="center" prop="warehouseName" />
        <el-table-column label="存储位置" align="center" prop="areaName" />
        <el-table-column label="产品名称" align="center" prop="productName" />
        <el-table-column label="最低库存" align="center" prop="minStock" />
        <el-table-column label="最高库存" align="center" prop="maxStock" />
        <el-table-column label="库存 " align="center" prop="quantity" />
        <el-table-column label="状态" align="center" >
          <template #default="scope">
            <dict-tag :options="wms_warehouse_product"
                      :value="scope.row.quantity > scope.row.maxStock ? '2' : scope.row.quantity < scope.row.minStock ? '1' : '0' " />
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="pd.total>0" :total="pd.total" v-model:page="pd.queryParams.pageNum"
                  v-model:limit="pd.queryParams.pageSize" @pagination="getList" />
    </el-card>
  </div>
</template>

<script setup lang="ts">
import {listWWP, warehouseProductVo} from '@/views/wms/warehouseProduct/api';
import WarehouseSelect from "@/views/wms/warehouse/select.vue";
import ProductSearch from "@/views/wms/product/search.vue";
import {ProductVO} from "@/views/wms/product/api";
import {wwpVo} from "@/views/wms/warehouse/api";

const queryFormRef = ref<ElFormInstance>();
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const {wms_warehouse_product} = toRefs<any>(proxy?.useDict('wms_warehouse_product'));

const pd = reactive({
  WWPList: <warehouseProductVo[]>([]),
  loading: true,
  showSearch: true,
  total: 0,
  queryParams: <warehouseProductVo>{
    pageNum: 1,
    pageSize: 10,
  },
});

/** 查询生产计划排程列表 */
async function getList() {
  pd.loading = true;
  const res = await listWWP(pd.queryParams);
  pd.WWPList = res.rows;
  pd.total = res.total;
  pd.loading = false;
}

/** 搜索按钮操作 */
function handleQuery() {
  pd.queryParams.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  queryFormRef.value?.resetFields();
  pd.queryParams.productId = undefined;
  handleQuery();
}

async function lineAdd(vo: ProductVO[]) {
  vo.forEach((v) => {
    pd.queryParams.productName = v.name;
    pd.queryParams.productId = v.id;
  });
}

onMounted(() => {
  getList();
});
</script>
